package ModelLayer;

import java.util.ArrayList;


/**
 * This class Represents an address book.
 * 
 * @author Sebastian K Munch
 * @version 5/11-12
 */
public class AddressBook
{
    private ArrayList<Person> personList;
    private static AddressBook instance;
    
    /**
     * This constructor creates a new instance of an empty AddressBook.
     */
    private AddressBook()
    {
        personList = new ArrayList<Person>();
    }
    
    /**
     * This method returns the instance of this class, in acordance with a singleton implementation.
     * 
     * @return  The instance of this class.
     */
    public static AddressBook getInstance()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }
    
    /**
     * The method returns a Person, indentified by that persons id.
     * 
     * @param   id  The id of the person to be returned, as a long.
     * 
     * return   The first person found indetified by the given id, as a Person.
     */
    public Person getPerson(long id)
    {
        Person p = null;
        boolean found = false;
        int i = 0;
        while(i < personList.size() && !found)
        {
            if(personList.get(i).getId()==id)
            {
                p = personList.get(i);
                found = true;
            }
            i++;
        }
        return p;
    }
    
    /**
     * This method creats and ads an instance of Person to the list of person represented by this class.
     * 
     * @param   id          The id that is to identify the person to be created. A long.
     * @param   firstName   The first name of the person to be created. A String.
     * @param   surname     The last name of the person to be created. A String.
     * @param   address     The address of the person to be created. A String.
     * @param   zipCode     The zip code of the person to be created. A String.
     * @param   town        The town of the person to be created. A String.
     * @param   phone       The phone number of the person to be created. A String.
     */
    public void createPerson(long id, String firstName, String surname, String address, String zipCode, String town, String phone)
    {
        personList.add(new Person(id, firstName, surname, address, zipCode, town, phone));
    }
    
    /**
     * This method deletes the first person found identified by the specific id.
     * 
     * @param   id      The id of the person to be deleted. A long.
     */
    public boolean deletePerson(long id)
    {
        boolean found = false;
        int i = 0;
        while(i < personList.size() && !found)
        {
            if(personList.get(i).getId()==id)
            {
                personList.remove(i);
                found = true;
            }
            i++;
        }
        return found;
    }
    
    /**
     * This method updates all the information of the first person found indetified by the given id.
     * 
     * @param   id          The id that identifies the person to be updated. A long.
     * @param   firstName   The new first name of the person. A String.
     * @param   surname     The new last name of the person. A String.
     * @param   address     The new address of the person. A String.
     * @param   zipCode     The new zip code of the person. A String.
     * @param   town        The new town of the person. A String.
     * @param   phone       The new phone number of the person. A String.
     */
    public boolean updatePerson(long id, String firstName, String surname, String address, String zipCode, String town, String phone)
    {
        boolean found = false;
        int i = 0;
        while(i < personList.size() && !found)
        {
            if(personList.get(i).getId()==id)
            {
                personList.get(i).setFirstName(firstName);
                personList.get(i).setSurName(surname);
                personList.get(i).setAddress(address);
                personList.get(i).setZip(zipCode);
                personList.get(i).setTown(town);
                personList.get(i).setPhone(phone);
                found = true;
            }
            i++;
        }
        return found;
    }
    
    /**
     * This method returns a list of all persons added to this class.
     * 
     * @return  A list of persons, as an ArrayList<Person>.
     */
    public ArrayList<Person> listAllPersons()
    {
        return personList;
    }
}
